import session from '../../utils/uitls';
import  axios from '../../utils/http';

import * as api from "../../utils/api"
//store里需要重新请求
const state = {
    user: session.getStorage('user') || {}
}
const actions = {
    //异步。
    loginAction({ commit }, obj) {

        console.log('loginAction')
        return new Promise((resolve, reject) => {
            console.log(obj)

            /*  let admin_account=obj.admin_account
              let admin_pwd=obj.admin_pwd
              console.log({admin_account,admin_pwd})*/
            sessionStorage.removeItem("user");
            sessionStorage.removeItem("token");
            axios({
                method: 'get',
                url: api.GET_LOGIN,
                params: obj
            }).then((data) => {
                console.log('登录信息返回')
                console.log(data)
                if (data.data.code === 200) {
                    let { admin_account, admin_name, admin_phone, admin_icon, role_id, role_name } = {...data.data.data[0] }
                    console.log({ admin_account, admin_name, admin_phone, admin_icon, role_id, role_name })
                    session.setStorage('user', { admin_account, admin_name, admin_phone, admin_icon, role_id, role_name })
                    session.setStorage('token', data.data.token)
                    commit('loginMutation', { admin_account, admin_name, admin_phone, admin_icon, role_id, role_name })
                    resolve(data.data)
                } else {
                    reject(data.data)
                }
            })
        })
    }
}
const mutations = {
    //同步。
    loginMutation(state, data) {
        console.log('进入loginMutation了吗')
        state.user = data
    }
}
export default ({
    state,
    actions,
    mutations
})